package com.team.mp.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.bo.DepartmentPageBo;
import com.team.mp.api.bo.TeamAddBo;
import com.team.mp.api.bo.TeamPageBo;
import com.team.mp.api.bo.TeamUpdateBo;
import com.team.mp.api.service.intf.TeamService;
import com.team.mp.mapper.dao.*;
import com.team.mp.mapper.dto.DepartmentDto;
import com.team.mp.mapper.dto.TeamDto;
import com.team.mp.mapper.entity.*;
import com.team.mp.vo.R;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 千锋郑州Java学科欢迎你
 * @program: Tproject
 * @description: 业务接口实现层
 * @author: xiaopi
 * @create: 2023-03-27 11:47:49
 */
@Service
public class TeamServiceImpl implements TeamService {
    /**
    *注入持久层对象*/
    @Resource
    private TeamDao dao;
    /**
     *注入department持久层对象*/
    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private DepartTeamDao departTeamDao;
    /**
     *注入teamTagDao持久层对象*/
    @Resource
    private TeamTagDao teamTagDao;

    /**
     *注入LabelDao持久层对象*/
    @Resource
    private LabelDao labelDao;

    /**
     *注入servicePackMapper持久层对象*/
    @Resource
    private ServicePackDao servicePackMapper;

    /**
     *注入TeamPackDao持久层对象*/
    @Resource
    private TeamPackDao teamPackDao;

    /**
     *注入doctorDao持久层对象*/
    @Resource
    private DoctorDao doctorDao;

    /**
     *注入docterTeamDao持久层对象*/
    @Resource
    private DocterTeamDao docterTeamDao;





    /**
    * 新增内容*/
    @Override
    public R save(TeamAddBo bo) {
        if(bo!=null){
            Team team = new Team();
            BeanUtil.copyProperties(bo,team);
            if(dao.insert(team) > 0){
                Team team1 = dao.selectOne(new QueryWrapper<Team>().eq("team_name", team.getTeamName()));
                Integer teamId = team1.getId();
                String departmentName = bo.getDepartment();
                Department department = departmentDao.selectOne(new QueryWrapper<Department>()
                        .eq("depart_name", departmentName));
//            将获取到的departmentId和TeamId存储到机构团队表中
                DepartTeam departTeam = new DepartTeam();
                departTeam.setDepartId(department.getId());
                departTeam.setTeamId(teamId);
                //插入机构团队表
                departTeamDao.insert(departTeam);

                //插入团队标签表
                List<String> labels = bo.getLabels();
                for (String item:labels) {
                    Integer labelId = labelDao.selectOne(new QueryWrapper<Label>().eq("name", item)).getId();
                    TeamTag teamTag = new TeamTag();
                    teamTag.setTid(teamId);
                    teamTag.setTagid(labelId);
                    teamTagDao.insert(teamTag);
                }
                //插入团队服务包表
                List<String> servePack = bo.getServePack();
                for (String pack:servePack) {
                    Integer packId = servicePackMapper.selectOne(new QueryWrapper<ServicePack>().eq("spname", pack)).getId();
                    TeamPack teamPack = new TeamPack();
                    teamPack.setPackId(packId);
                    teamPack.setTeamId(teamId);
                    teamPackDao.insert(teamPack);
                }
                //插入团队成员表
                List<String> doctors = bo.getDoctor();
                for (String doctor:doctors) {
                    Integer doctorId = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("doctor_name", doctor)).getId();
                    DocterTeam docterTeam = new DocterTeam();
                    docterTeam.setTeamId(teamId);
                    docterTeam.setDocterId(doctorId);
                    docterTeamDao.insert(docterTeam);
                }
                return R.ok();
            }else {
                return R.fail();
            }

    }
        return R.fail("亲，新增异常！");
    }
    /**
    * 修改内容*/
    @Override
    public R update(TeamUpdateBo bo) {
        if (bo != null) {
            Team team = new Team();
            BeanUtil.copyProperties(bo, team);
            if (dao.updateById(team)>0) {
                int teamId = bo.getId();
                String departmentName = bo.getDepartment();
                Department department = departmentDao.selectOne(new QueryWrapper<Department>()
                        .eq("depart_name", departmentName));
//            将获取到的departmentId和TeamId存储到机构团队表中
                DepartTeam departTeam = new DepartTeam();
                departTeam.setDepartId(department.getId());
                departTeam.setTeamId(teamId);
                DepartTeam departTeam1 = departTeamDao.selectOne(new QueryWrapper<DepartTeam>().eq("depart_id", department.getId()).eq("team_id", teamId));
                departTeam.setId(departTeam1.getId());
                //修改机构团队表
                departTeamDao.updateById(departTeam);

                //修改团队标签表
                List<String> labels = bo.getLabels();
                teamTagDao.delete(new QueryWrapper<TeamTag>().eq("team_id",teamId));
                for (String item:labels) {
                    Integer labelId = labelDao.selectOne(new QueryWrapper<Label>().eq("name", item)).getId();
                    TeamTag teamTag = new TeamTag();
                    teamTag.setTid(teamId);
                    teamTag.setTagid(labelId);
                    teamTagDao.insert(teamTag);
                }

//                for (String item : labels) {
//                    Integer labelId = labelDao.selectOne(new QueryWrapper<Label>().eq("name", item)).getId();
//                    TeamTag teamTag = new TeamTag();
//                    teamTag.setTid(teamId);
//                    teamTag.setTagid(labelId);
//                    TeamTag teamTag1 = teamTagDao.selectOne(new QueryWrapper<TeamTag>().eq("tid", teamId).eq("tagid", labelId));
//                    teamTag.setId(teamTag1.getId());
//                    teamTagDao.updateById(teamTag);
//                }
                //修改团队服务包表
                List<String> servePack = bo.getServePack();
                teamPackDao.delete(new QueryWrapper<TeamPack>().eq("team_id",teamId));
                for (String pack:servePack) {
                    Integer packId = servicePackMapper.selectOne(new QueryWrapper<ServicePack>().eq("spname", pack)).getId();
                    TeamPack teamPack = new TeamPack();
                    teamPack.setPackId(packId);
                    teamPack.setTeamId(teamId);
                    teamPackDao.insert(teamPack);
                }
//                for (String pack : servePack) {
//                    Integer packId = servicePackMapper.selectOne(new QueryWrapper<ServicePack>().eq("spname", pack)).getId();
//                    TeamPack teamPack = new TeamPack();
//                    teamPack.setPackId(packId);
//                    teamPack.setTeamId(teamId);
//                    TeamPack teamPack1 = teamPackDao.selectOne(new QueryWrapper<TeamPack>().eq("pack_id", packId).eq("team_id", teamId));
//                    teamPack.setId(teamPack1.getId());
//                    teamPackDao.updateById(teamPack);
//                }
                //修改团队成员表
                List<String> doctors = bo.getDoctor();
                docterTeamDao.delete(new QueryWrapper<DocterTeam>().eq("team_id",teamId));
                for (String doctor:doctors) {
                    Integer doctorId = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("doctor_name", doctor)).getId();
                    DocterTeam docterTeam = new DocterTeam();
                    docterTeam.setTeamId(teamId);
                    docterTeam.setDocterId(doctorId);
                    docterTeamDao.insert(docterTeam);
                }
//                for (String doctor : doctors) {
//                    Integer doctorId = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("doctor_name", doctor)).getId();
//                    DocterTeam docterTeam = new DocterTeam();
//                    docterTeam.setTeamId(teamId);
//                    docterTeam.setDocterId(doctorId);
//                    DocterTeam docterTeam1 = docterTeamDao.selectOne(new QueryWrapper<DocterTeam>().eq("team_id", teamId).eq("docter_id", doctorId));
//                    docterTeam.setId(docterTeam1.getId());
//                    docterTeamDao.updateById(docterTeam);
//                }
                return R.ok();
            }else {
                return R.fail();
        }
    }
        return R.fail("亲，修改异常！");
    }
    /**
    *删除 */
    @Override
    public R del(long id){
        if(id>0){
            int i=dao.deleteById(id);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
    * 查询详情*/
    @Override
    public R queryById(long id){
        return R.ok(dao.selectById(id));
    }

    /**
    * 查询 所有数据*/
    @Override
    public R queryAll() {
      return R.ok(dao.selectList(null));
    }

    @Override
    public R getTeamList(TeamPageBo bo) {
        String departName = bo.getDepartName();
        QueryWrapper<Team> wrapper = new QueryWrapper<>();
//        if(StringUtils.hasLength(departName)){
//            wrapper.like("d1.depart_name",departName);
//        }
//        Page<DepartmentDto> page = new Page<>();
//        page.setCurrent(bo.getCurrentPage());
//        page.setSize(bo.getPageSize());
        return null;
    }

    @Override
    public R getTeam(TeamPageBo bo) {
        Page<TeamDto> page = new Page<>();
        page.setCurrent(bo.getCurrentPage());
        page.setSize(bo.getPageSize());
        Page<TeamDto> page1 = dao.page(page);
        return R.ok(page1);
    }
}
